package com.deprice4u_client;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

import com.deprice4u_client.products_on_sale.SocialListViewActivity;
import com.deprice4u_client.products_on_sale.SocialTiledUIActivity;

public class MainTabsFragment extends Fragment implements OnTabChangeListener{

	private static final String TAG = "DePrice4U";
	public static final String TAB_ON_SALE = "onSale";
	public static final String TAB_ON_REQUEST = "onRequest";
	
	public String queryFromSearch;
	public boolean isFromSearch = false;

	private View mRoot;
	private TabHost mTabHost;
	private int mCurrentTab;

	@Override
	// 첫번 째로 불린다.
	public void onAttach(Activity activity) {
	//	Log.i("SM","onAttach");
		super.onAttach(activity);  
	}

	@Override
	// 두번 째로 불린다.
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
	//	Log.i("SM","onCreateView");
		mRoot = inflater.inflate(R.layout.main_tabs_fragement, null);
		mTabHost = (TabHost) mRoot.findViewById(android.R.id.tabhost);
		setupTabs();
		return mRoot;   
	}

	@Override
	// 세번 째로 불린다.
	public void onActivityCreated(Bundle savedInstanceState) {
	//	Log.i("SM","onAcitivityCreated");
		super.onActivityCreated(savedInstanceState);
		setRetainInstance(true);

		mTabHost.setOnTabChangedListener(this);
		mTabHost.setCurrentTab(mCurrentTab);
		// manually start loading stuff in the first tab
		updateTab(TAB_ON_SALE, R.id.tab_on_sale);
	}

	private void setupTabs() {
		mTabHost.setup(); // important!
		mTabHost.addTab(newTab(TAB_ON_SALE, R.string.tab_tag_on_sale, R.id.tab_on_sale));
		mTabHost.addTab(newTab(TAB_ON_REQUEST, R.string.tab_tag_on_request, R.id.tab_on_request));
		

		mRoot.findViewById(R.id.ll_for_btns).setVisibility(View.GONE); 	
		mRoot.findViewById(R.id.btn1_for_sort).setVisibility(View.GONE); 
		mRoot.findViewById(R.id.btn2_for_sort).setVisibility(View.GONE); 
		mRoot.findViewById(R.id.btn3_for_sort).setVisibility(View.GONE); 
		
		//Log.i("SM","setup tabs");
	}

	private TabSpec newTab(String tag, int labelId, int tabContentId) {
		//Log.d(TAG, "buildTab(): tag=" + tag);

		LayoutInflater lif = LayoutInflater.from( getActivity() );
		View indicator = lif.inflate(R.layout.tab, (ViewGroup) mRoot.findViewById(android.R.id.tabs), false);
		
		((TextView) indicator.findViewById(R.id.text)).setText(labelId);

		TabSpec tabSpec = mTabHost.newTabSpec(tag);
		tabSpec.setIndicator(indicator);
		tabSpec.setContent(tabContentId);
		return tabSpec;
	}

	public void onTabChanged(String tabId){
		//Log.d(TAG, "onTabChanged(): tabId=" + tabId);
		if (TAB_ON_SALE.equals(tabId)) {
			updateTab(tabId, R.id.tab_on_sale);
			mCurrentTab = 0;
			return;
		}
		if (TAB_ON_REQUEST.equals(tabId)) {
			updateTab(tabId, R.id.tab_on_request);
			mCurrentTab = 1;
			return;  
		}
	}

	public void updateTab(String tabId, int placeholder) {
		FragmentManager fm = getFragmentManager();
		
	/*	if(tabId == TAB_ON_SALE){
			mRoot.findViewById(R.id.ll_for_btns).setVisibility(View.VISIBLE); 	
			mRoot.findViewById(R.id.btn1_for_sort).setVisibility(View.VISIBLE); 
			mRoot.findViewById(R.id.btn2_for_sort).setVisibility(View.VISIBLE); 
			mRoot.findViewById(R.id.btn3_for_sort).setVisibility(View.VISIBLE); 
		}else if(tabId == TAB_ON_REQUEST){
			mRoot.findViewById(R.id.ll_for_btns).setVisibility(View.GONE); 	
			mRoot.findViewById(R.id.btn1_for_sort).setVisibility(View.GONE); 
			mRoot.findViewById(R.id.btn2_for_sort).setVisibility(View.GONE); 
			mRoot.findViewById(R.id.btn3_for_sort).setVisibility(View.GONE); 		
		}*/
		
		if (fm.findFragmentByTag(tabId) == null) {
			if(tabId == TAB_ON_SALE){
				fm.beginTransaction().replace(placeholder, new SocialListViewActivity(), tabId).commit();
			}else if(tabId == TAB_ON_REQUEST){
//				fm.beginTransaction().replace(placeholder, new TiledUIActivity(), tabId).commit();		
				fm.beginTransaction().replace(placeholder, new SocialTiledUIActivity(), tabId).commit();		
			}
			
		}
	}


}
